package org.seafloor.system.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.seafloor.common.model.SystemConstants;
import org.seafloor.system.model.UserVO;
import org.seafloor.system.service.UserService;

public class LoginAction {
	private UserVO userVO ;
	private UserService userService;
	private String errorMsg;
	HttpServletRequest request;
	
	
	public String loginIn() {
		System.out.println("访问Action！loginUser");
		try {
			String loginName = userVO.getLoginName();
			String loginPswd = userVO.getLoginPswd();
			
			request=ServletActionContext.getRequest();
			HttpSession session=ServletActionContext.getRequest().getSession();
			
			if(null==loginName || "".equals(loginName.trim())){
				this.errorMsg="登录用户名不能为空";
				return "login";
			}
			if(null==loginPswd || "".equals(loginPswd.trim())){
				this.errorMsg="登录密码不能为空";
				return "login";
			}
			UserVO u1=null;
			u1=userService.findUserByLoginName(userVO);
			if(null==u1){
				this.errorMsg="登录用户名不存在";
				return "login";
			}
			if(!loginPswd.equals(u1.getLoginPswd())){
				this.errorMsg="密码错误，请重新输入";
				return "login";
			}
			if(u1.getStatus()!=0){
				this.errorMsg="账号未启用或已锁定，请联系管理员";
				return "login";
			}
			
			UserVO u2=new UserVO();
			u2.setUserId(u1.getUserId());
			userService.updateUserLastLogin(u2);
			
			session.setAttribute(SystemConstants.SESSION_USERMESSAGE, u1);
//			errorMsg="登录成功";

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	public String loginOut(){
		request = ServletActionContext.getRequest() ;
		if(null != request.getSession() ){
			request.getSession().invalidate();
		}
		return "login";
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserVO getUserVO() {
		return userVO;
	}

	public void setUserVO(UserVO userVO) {
		this.userVO = userVO;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	
}
